<template>
  <div class="blessing-slider" @mouseenter="isHover = true" @mouseleave="isHover = false">
    <div class="container-content">
      <div class="blessing-wrapper position-relative">
        <!-- center slider -->
        <transition-group
          tag="div"
          :name="direction > 0 ? 'slide' : 'slide-invert'"
          class="slide-block"
          @touchstart="onTouchstart"
          @touchend="onTouchend"
        >
          <div
            v-for="(item, idx) in apiList"
            v-show="idx === activeIdx"
            class="slide-block__image"
            :key="`${item.name}-${item.label}-${item.image_url}-${idx}`"
            ref="image"
            @transitionstart="isTransitioning = true"
            @transitionend="isTransitioning = false"
          >
            <img :src="item.image_url" :alt="item.name" />
          </div>
          <div
            class="i page-arrow__left"
            key="arrow__left"
            @click="changeSlide(activeIdx - 1)"
          ></div>
          <div
            class="i page-arrow__right"
            key="arrow__right"
            @click="changeSlide(activeIdx + 1)"
          ></div>
        </transition-group>
        <div
          v-if="activeSlide"
          class="slide-card"
          :style="colorSet"
          @touchstart="onTouchstart"
          @touchend="onTouchend"
          @click="cardClickHandler(activeSlide)"
        >
          <div class="slide-card__wrapper">
            <div class="slide-card__head">
              <h5
                class="slide-card__name text-uppercase"
                v-html="activeSlide.name"
              ></h5>
              <div
                class="slide-card__label"
                v-html="activePersonTypeLabel"
              ></div>
            </div>
            <div
              class="slide-card__body"
              v-html="activeSlide.message"
            ></div>
          </div>
        </div>

        <!-- prev slider -->
        <transition-group
          tag="div"
          :name="direction > 0 ? 'slide' : 'slide-invert'"
          class="slide-block slide-block__prev"
        >
          <div
            v-for="(item, idx) in apiList"
            v-show="idx === prevIndex"
            class="slide-block__image"
            :key="`${item.name}-${item.label}-${item.image_url}-${idx}`"
            ref="image"
            @transitionstart="isTransitioning = true"
            @transitionend="isTransitioning = false"
          >
            <img :src="item.image_url" :alt="item.name" />
          </div>
        </transition-group>

        <!-- next slider -->
        <transition-group
          tag="div"
          :name="direction > 0 ? 'slide' : 'slide-invert'"
          class="slide-block slide-block__next"
        >
          <div
            v-for="(item, idx) in apiList"
            v-show="idx === nextIndex"
            class="slide-block__image"
            :key="`${item.name}-${item.label}-${item.image_url}-${idx}`"
            ref="image"
            @transitionstart="isTransitioning = true"
            @transitionend="isTransitioning = false"
          >
            <img :src="item.image_url" :alt="item.name" />
          </div>
        </transition-group>

        <!-- slide buttons -->
        <button
          v-show="apiList.length > 1"
          type="button"
          class="blessing-btn blessing-btn--prev"
          @click="changeSlide(activeIdx - 1)"
        >
          <div class="i"></div>
        </button>
        <button
          v-show="apiList.length > 1"
          type="button"
          class="blessing-btn blessing-btn--next"
          @click="changeSlide(activeIdx + 1)"
        >
          <div class="i"></div>
        </button>
      </div>
    </div>

    <LightboxBlessingMessage v-model:visible="visible" :componentProps="focusObj" />
    <Operate v-if="!isPreview" @handle-edit="showSetting = true" @handle-delete="handleDelete" @handle-copy="handleCopy" @handle-paste="handlePaste"></Operate>
  </div>

  <Setting v-if="!isPreview" v-model:show="showSetting" :data="modelValue"></Setting>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import LightboxBlessingMessage from '@/common/LightboxBlessingMessage/index.vue'
import Operate from '@/common/Operate/index.vue'
import Setting from './setting.vue'
import { randomString } from '@/utils/common'
import { http } from '@/utils/http'

defineOptions({
  name: 'BlessingSlider'
})

const props = defineProps({
  modelValue: {
    type: Object,
    default: () => {}
  },

  // 是否预览
  isPreview: {
    type: Boolean,
    default: true
  },

  website: {
    type: String,
    default: 'ywies-tx'
  },

  locale: {
    type: String,
    default: 'sc'
  }
})

const emit = defineEmits(['update:modelValue', 'handleDelete', 'handleCopy', 'handlePaste', 'showLightboxVideo', 'showLightboxImage'])

const handleInit = () => {
  if (props.modelValue && props.modelValue.data) return // 有数据不用初始化
  const data = {
    id: props.modelValue?.id || randomString(),
    type: 'BlessingSlider',
    api: props.modelValue?.api || '/blessing-board/blessing-messages/featured?count=6',
  }
  emit('update:modelValue', data)
} 

const isHover = ref<boolean>(false);
const showSetting = ref<boolean>(false);

const handleDelete = () => {
  emit('handleDelete', props.modelValue.id)
}

const handleCopy = () => {
  emit('handleCopy', props.modelValue)
}

const handlePaste = (type: string) => {
  emit('handlePaste', type)
}

handleInit()

const image = ref<HTMLElement[]>([])
const apiList = ref<any[]>([])
const activeIdx = ref(0)
const direction = ref(1)
const touchstartX = ref(0)
const touchendX = ref(0)
const focusObj = ref<any>(null)
const isTransitioning = ref(false)

const total = computed(() => apiList.value.length)
const activeSlide = computed(() => apiList.value[activeIdx.value])
const prevIndex = computed(() =>
  activeIdx.value - 1 < 0 ? total.value - 1 : activeIdx.value - 1
)
const nextIndex = computed(() =>
  activeIdx.value + 1 >= total.value ? 0 : activeIdx.value + 1
)
const colorType = computed(() => activeIdx.value % 3)

const colorSet = computed(() => {
  const colorMap = [
    {
      '--bg-color': '#0032A0',
      '--border-color': '#FF0044',
    },
    {
      '--bg-color': '#FF8200',
      '--border-color': '#0032A0',
    },
    {
      '--bg-color': '#FF0044',
      '--border-color': '#D2D6DC',
    },
  ]
  return colorMap[colorType.value]
})

const personTypeList = computed(() => [
  {
    key: 'parent',
    name: '家长',
    value: '家长',
  },
  {
    key: 'alumni',
    name: '校友',
    value: '校友',
  },
  {
    key: 'student',
    name: '学生',
    value: '学生',
  },
  {
    key: 'faculty-staff',
    name: '教学人员/行政人员',
    value: '教学人员/行政人员',
  },
  {
    key: 'others',
    name: '其他',
    value: '其他',
  },
])

const activePersonTypeLabel = computed(() => {
  if (!activeSlide.value) return null
  if (activeSlide.value.person_type === 'others') return activeSlide.value?.label
  return (
    personTypeList.value.find((v: any) => v.key === activeSlide.value?.person_type)
      ?.name ?? activeSlide.value?.label
  )
})

const changeSlide = (idx: number) => {
  direction.value = idx > activeIdx.value ? 1 : -1
  activeIdx.value = (idx + total.value) % total.value
}

const checkDirection = () => {
  if (touchendX.value < touchstartX.value)
    changeSlide(activeIdx.value + 1)
  if (touchendX.value > touchstartX.value)
    changeSlide(activeIdx.value - 1)
}

const onTouchstart = (e: TouchEvent) => {
  touchstartX.value = e.changedTouches[0].screenX
}

const onTouchend = (e: TouchEvent) => {
  touchendX.value = e.changedTouches[0].screenX
  checkDirection()
}

const visible = ref(false)
const cardClickHandler = (item: any) => {
  focusObj.value = item
  visible.value = true
}

const getList = async () => {
  const config = {
    headers: {
      website: `${props.website}-${props.locale}`,
    },
  }
  const url = `${props.modelValue?.api}`
  try {
    const { data } = await http.get(url, config)
    apiList.value = data.data
  } catch (e) {
    apiList.value = []
  }
}

onMounted(() => {
  getList()
})
</script>

<style lang="scss" scoped>
.blessing-slider {
  position: relative;
  margin-bottom: 120px;
}

@media (max-width: 767.98px) {
  .blessing-slider {
    margin-bottom: 0;
  }
}

@media (min-width: 1280px) {
  .blessing-slider .container-content {
    --max-width: clamp(900px, calc((100vw - 1280px) / 2.177 + 900px), 1194px);
  }
}

@media (max-width: 767.98px) {
  .blessing-slider .container-content {
    max-width: 100vw;
    padding: 0;
  }
}

.blessing-slider .blessing-wrapper {
  position: relative;
}

.blessing-slider .blessing-wrapper .slide-block {
  position: relative;
  width: clamp(460px, calc((100vw - 1280px) / 4.238 + 460px), 611px);
  height: clamp(352px, calc((100vw - 1280px) / 5.517 + 352px), 468px);
  overflow: hidden;
  max-width: 100%;
}

@media (max-width: 767.98px) {
  .blessing-slider .blessing-wrapper .slide-block {
    margin: 0 auto;
    width: calc(100% - 24px);
    height: 69.375vw;
  }
}

.blessing-slider .blessing-wrapper .slide-block__prev,
.blessing-slider .blessing-wrapper .slide-block__next {
  position: absolute;
  top: 0;
}

.blessing-slider .blessing-wrapper .slide-block__prev {
  left: 0;
  transform: translateX(calc(-100% - 50px));
}

@media (min-width: 1280px) {
  .blessing-slider .blessing-wrapper .slide-block__prev {
    transform: translateX(calc(-100% - 67px));
  }
}

.blessing-slider .blessing-wrapper .slide-block__next {
  right: 0;
  transform: translateX(calc(100% + 50px));
}

@media (min-width: 1280px) {
  .blessing-slider .blessing-wrapper .slide-block__next {
    transform: translateX(calc(100% + 67px));
  }
}

.blessing-slider .blessing-wrapper .slide-block__image {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  overflow: hidden;
  will-change: transform;
}

.blessing-slider .blessing-wrapper .slide-block__image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.blessing-slider .blessing-wrapper .slide-block .page-arrow__left,
.blessing-slider .blessing-wrapper .slide-block .page-arrow__right {
  position: absolute;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  overflow: hidden;
  cursor: pointer;
  top: 50%;
  z-index: 20;
  display: none;
}

@media (max-width: 767.98px) {
  .blessing-slider .blessing-wrapper .slide-block .page-arrow__left,
  .blessing-slider .blessing-wrapper .slide-block .page-arrow__right {
    display: inline-block;
  }
}

.blessing-slider .blessing-wrapper .slide-block .page-arrow__left::before,
.blessing-slider .blessing-wrapper .slide-block .page-arrow__right::before {
  content: '';
  width: 24px;
  height: 24px;
  background: url('@/assets/icon-arrow-pagination-right.svg') center center no-repeat;
  background-size: 100% auto;
  transition: transform 0.4s ease;
  filter: brightness(0) invert(1);
}

.blessing-slider .blessing-wrapper .slide-block .page-arrow__left {
  transform: translateY(-50%) scale(-1);
  left: 0;
}

.blessing-slider .blessing-wrapper .slide-block .page-arrow__right {
  transform: translateY(-50%);
  right: 0;
}
.blessing-slider .blessing-wrapper .slide-card {
  padding: 25px;
  background-color: var(--bg-color);
  border-radius: 10px;
  position: absolute;
  right: 0;
  top: clamp(126px, calc((100vw - 1280px) / 15.238 + 126px), 168px);
  width: clamp(556px, calc((100vw - 1280px) / 3.404 + 556px), 744px);
  z-index: 10;
  cursor: pointer;
  transform: scale(1);
  transition: background-color 0.3s ease-in-out, transform 0.3s ease-in-out;
}

@media (max-width: 1439.98px) {
  .blessing-slider .blessing-wrapper .slide-card {
    padding: 19px;
  }
}

@media (max-width: 767.98px) {
  .blessing-slider .blessing-wrapper .slide-card {
    border-radius: 0;
    position: relative;
    right: unset;
    top: unset;
    width: 89%;
    padding: 12px;
    max-width: 556px;
    margin-right: 0;
    margin-left: auto;
    margin-top: -44px;
  }
}

@media (min-width: 768px) {
  .blessing-slider .blessing-wrapper .slide-card:hover {
    transform: scale(0.95);
  }
}

.blessing-slider .blessing-wrapper .slide-card__wrapper {
  padding: 37.5px 41px 52px;
  border: 1.3px solid var(--border-color);
  border-radius: 10px;
  color: #ffffff;
  transition: border 0.3s ease-in-out;
}

@media (max-width: 1439.98px) {
  .blessing-slider .blessing-wrapper .slide-card__wrapper {
    padding: 26px 31px;
  }
}

@media (max-width: 767.98px) {
  .blessing-slider .blessing-wrapper .slide-card__wrapper {
    border-radius: 0;
    padding: 12px 15px 12px 17px;
  }
}

.blessing-slider .blessing-wrapper .slide-card__head {
  display: flex;
  align-items: center;
}

.blessing-slider .blessing-wrapper .slide-card__head > * {
  padding-right: 14px;
}

.blessing-slider .blessing-wrapper .slide-card__head > *:last-child {
  padding-right: 0;
}

.blessing-slider .blessing-wrapper .slide-card__head > *:not(:first-child) {
  display: flex;
  align-items: center;
  font-size: 18px;
  line-height: 1.5;
  font-weight: 400;
}

@media (max-width: 767.98px) {
  .blessing-slider .blessing-wrapper .slide-card__head > *:not(:first-child) {
    font-size: 16px;
  }
}

.blessing-slider .blessing-wrapper .slide-card__head > *:not(:first-child)::before {
  content: '';
  display: inline-block;
  width: 2px;
  height: 17px;
  background-color: #ffffff;
  margin-right: 14px;
  border-radius: 2px;
}

.blessing-slider .blessing-wrapper .slide-card__name {
  font-size: 24px;
  line-height: 1.33;
  font-weight: 700;
}

@media (min-width: 1440px) {
  .blessing-slider .blessing-wrapper .slide-card__name {
    font-size: 36px;
    line-height: 1.22;
  }
}

.blessing-slider .blessing-wrapper .slide-card__label {
  font-size: 18px;
  line-height: 1.33;
  font-weight: 400;
}

@media (max-width: 1439.98px) {
  .blessing-slider .blessing-wrapper .slide-card__label {
    font-size: 16px;
    line-height: 1.5;
  }
}

@media (max-width: 767.98px) {
  .blessing-slider .blessing-wrapper .slide-card__label {
    font-size: 14px;
    line-height: 1.29;
  }
}

.blessing-slider .blessing-wrapper .slide-card__body {
  margin-top: 15px;
  height: calc(1.33em * 4);
  display: -webkit-box;
  -webkit-line-clamp: 4;
  line-clamp: 4;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

@media (min-width: 1440px) {
  .blessing-slider .blessing-wrapper .slide-card__body {
    margin-top: 22px;
    height: clamp(146px, calc((100vw - 1280px) / 45.714 + 146px), 160px);
    margin-bottom: 6px;
  }
}

@media (max-width: 767.98px) {
  .blessing-slider .blessing-wrapper .slide-card__body {
    margin-top: 9px;
    height: unset;
    max-height: calc(1.5em * 5);
    -webkit-line-clamp: 5;
    line-clamp: 5;
  }
}

.blessing-slider .blessing-wrapper .slide-card__body,
.blessing-slider .blessing-wrapper .slide-card__body :deep(*) {
  font-size: 30px;
  line-height: 1.33;
  font-weight: 400;
}

@media (max-width: 1439.98px) {
  .blessing-slider .blessing-wrapper .slide-card__body,
  .blessing-slider .blessing-wrapper .slide-card__body :deep(*) {
    font-size: 26px;
    line-height: 1.38;
  }
}

@media (max-width: 767.98px) {
  .blessing-slider .blessing-wrapper .slide-card__body,
  .blessing-slider .blessing-wrapper .slide-card__body :deep(*) {
    font-size: 18px;
    line-height: 1.5;
  }
}

.blessing-slider .blessing-wrapper .blessing-btn {
  position: absolute;
  top: 50%;
  width: 45px;
  height: 45px;
  border-radius: 50%;
  background-color: #ffffff;
  border: none;
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.12);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background-color 0.2s ease-in-out;
  z-index: 20;
}

@media (max-width: 767.98px) {
  .blessing-slider .blessing-wrapper .blessing-btn {
    display: none;
  }
}

.blessing-slider .blessing-wrapper .blessing-btn:hover {
  background-color: var(--text-color--primary, #0032A0);
}

.blessing-slider .blessing-wrapper .blessing-btn:hover .i::before {
  filter: brightness(0) invert(1);
}

.blessing-slider .blessing-wrapper .blessing-btn .i::before {
  width: 24px;
  height: 24px;
  background: url("~static/images/icons/icon-arrow-slider.svg") no-repeat center / 100% auto;
  transition: filter 0.2s ease-in-out;
}

.blessing-slider .blessing-wrapper .blessing-btn--prev {
  left: 0;
  transform: translate(calc(-100% - 30px), -50%);
}

@media (max-width: 1023.98px) {
  .blessing-slider .blessing-wrapper .blessing-btn--prev {
    transform: translate(-24px, -50%);
  }
}

@media (min-width: 1280px) {
  .blessing-slider .blessing-wrapper .blessing-btn--prev {
    transform: translate(calc(-100% - 45px), -50%);
  }
}

.blessing-slider .blessing-wrapper .blessing-btn--prev .i::before {
  transform: scale(-1);
}

.blessing-slider .blessing-wrapper .blessing-btn--next {
  right: 0;
  transform: translate(calc(100% + 30px), -50%);
}

@media (max-width: 1023.98px) {
  .blessing-slider .blessing-wrapper .blessing-btn--next {
    transform: translate(24px, -50%);
  }
}

@media (min-width: 1280px) {
  .blessing-slider .blessing-wrapper .blessing-btn--next {
    transform: translate(calc(100% + 45px), -50%);
  }
}

.slide-enter-active,
.slide-leave-active,
.slide-invert-enter-active,
.slide-invert-leave-active {
  transition: transform 0.8s ease-in-out;
}

.slide-enter-active,
.slide-invert-enter-active {
  z-index: 2;
}

.slide-enter {
  transform: translate(100%);
}

.slide-invert-enter {
  transform: translate(-100%);
}

.slide-leave-to,
.slide-invert-leave-to {
  transform: translate(0) scale(1.4);
}
</style>